package com.wswy.wzcx.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.ArrayRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.DrawableMarginSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;
import com.wswy.wzcx.R;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subscribers.ResourceSubscriber;
import java.util.concurrent.TimeUnit;

public class FullHintAlertDialog extends Dialog implements OnClickListener{

  public FullHintAlertDialog(@NonNull Context context) {
    super(context);
  }


  private String title;
  private CharSequence message;

  private CharSequence message2;

  private CharSequence message3;

  private String leftBtnText;
  private String rightBtnText;

  private View.OnClickListener leftOnClickListener;
  private View.OnClickListener rightOnClickListener;

  private int delayNext = 0;



  public FullHintAlertDialog setDialogTitle(String title){
    this.title = title;
    return this;
  }

  public FullHintAlertDialog setDialogTitle(@StringRes int titleResId){
    this.title = getContext().getString(titleResId);
    return this;
  }

  public FullHintAlertDialog setDialogMessage(String message){
    this.message = message;
    return this;
  }

  public FullHintAlertDialog setDialogMessage(@StringRes int messageResId){
    this.message = getContext().getString(messageResId);
    return this;
  }

  public FullHintAlertDialog setDialogMessage(@StringRes int messageResId,@StringRes int msgResId2){

    this.message = getContext().getString(messageResId);
    this.message2 = getContext().getString(msgResId2);
    return this;
  }

  public FullHintAlertDialog setDialogMessage(@StringRes int messageResId,@StringRes int msgResId2,@StringRes int msgResId3){

    this.message = getContext().getString(messageResId);
    this.message2 = getContext().getString(msgResId2);
    this.message3 = getContext().getString(msgResId3);
    return this;
  }

  public FullHintAlertDialog setDelayNext(int next){
    delayNext = next;
    return this;
  }

//  private CharSequence insertDot(CharSequence text){
//    SpannableString redDot = new SpannableString("●  ");
//    redDot.setSpan(new VerticalCenterSpan(8,ContextCompat.getColor(getContext(),R.color.accentColor3)),0,1,SpannableString.SPAN_INCLUSIVE_EXCLUSIVE);
//
//    SpannableStringBuilder sb = new SpannableStringBuilder();
//    sb.append(redDot);
//    sb.append(text);
//    return sb;
//  }

  public FullHintAlertDialog setLeftText(String leftBtnText,View.OnClickListener clickListener){
    this.leftBtnText = leftBtnText;
    leftOnClickListener = clickListener;
    return this;
  }

  public FullHintAlertDialog setLeftText(@StringRes int leftBtnTextResId,View.OnClickListener clickListener){
    this.leftBtnText = getContext().getString(leftBtnTextResId);
    leftOnClickListener = clickListener;
    return this;
  }


  public FullHintAlertDialog setRightText(String rightBtnText,View.OnClickListener clickListener){
    this.rightBtnText = rightBtnText;
    rightOnClickListener = clickListener;
    return this;
  }

  public FullHintAlertDialog setRightText(@StringRes int rightBtnTextResId,View.OnClickListener clickListener){
    this.rightBtnText = getContext().getString(rightBtnTextResId);
    rightOnClickListener = clickListener;
    return this;
  }


  private TextView tvNext;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    setContentView(R.layout.dialog_full_hint_alert);

    TextView tvTitle = findViewById(R.id.tv_title);
    TextView tvMessage = findViewById(R.id.tv_message_1);
    TextView tvMessage2 = findViewById(R.id.tv_message_2);
    TextView tvMessage3 = findViewById(R.id.tv_message_3);

    //TextView btnLeft = findViewById(R.id.btn_cancel);
    tvNext = findViewById(R.id.btn_next);

    if(title != null){
      tvTitle.setText(title);
    }
    tvMessage.setText(message);

    if (TextUtils.isEmpty(message2)){
      findViewById(R.id.fl_msg2).setVisibility(View.GONE);
    }else {
      findViewById(R.id.fl_msg2).setVisibility(View.VISIBLE);
      tvMessage2.setText(message2);
    }

    if (TextUtils.isEmpty(message3)){
      findViewById(R.id.fl_msg3).setVisibility(View.GONE);
    }else {
      findViewById(R.id.fl_msg3).setVisibility(View.VISIBLE);
      tvMessage3.setText(message3);
    }
    //btnLeft.setText(leftBtnText);
    tvNext.setText(rightBtnText);

    //btnLeft.setOnClickListener(this);
    tvNext.setOnClickListener(this);


    findViewById(R.id.img_close).setOnClickListener(this);

  }

  @Override
  public void onClick(View v) {
    dismiss();
    int id = v.getId();
    if(id == R.id.btn_cancel || id == R.id.img_close){

      if(leftOnClickListener != null){
        leftOnClickListener.onClick(v);
      }
    }else if(id == R.id.btn_next){
      if(rightOnClickListener != null){
        rightOnClickListener.onClick(v);
      }
    }
  }

  @Override
  public void show() {
    super.show();
    LayoutParams layoutParams = getWindow().getAttributes();
    layoutParams.gravity=Gravity.BOTTOM;
    layoutParams.width= LayoutParams.MATCH_PARENT;
    layoutParams.height= LayoutParams.WRAP_CONTENT;
    getWindow().setWindowAnimations(R.style.DialogBottom);

    getWindow().getDecorView().setPadding(0, 0, 0, 0);

    getWindow().setAttributes(layoutParams);
  }

  @Override
  protected void onStart() {
    super.onStart();

    if (delayNext > 0){

      startTimer();
    }

  }

  @Override
  protected void onStop() {
    super.onStop();
    if (timerSubscriber != null){
      timerSubscriber.dispose();
    }
  }

  private ResourceSubscriber<Long> timerSubscriber;

  private void startTimer(){


    if (timerSubscriber != null){
      timerSubscriber.dispose();
    }

    timerSubscriber = new ResourceSubscriber<Long>() {

      @Override
      protected void onStart() {
        super.onStart();
        tvNext.setEnabled(false);
      }

      @Override
      public void onNext(Long t) {

        tvNext.setText(rightBtnText+" ("+(5-t)+"s)");
      }

      @Override
      public void onError(Throwable t) {
        tvNext.setText(rightBtnText);
        tvNext.setEnabled(true);
      }

      @Override
      public void onComplete() {
        tvNext.setText(rightBtnText);
        tvNext.setEnabled(true);
      }

    };

    Flowable.intervalRange(0, 5, 0, 1, TimeUnit.SECONDS)
        .subscribeOn(AndroidSchedulers.mainThread())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(timerSubscriber);

  }


}
